package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Repairrecord;
import com.woniuxy.caradmin.entity.vo.RepairrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.RepairrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/repairrecord")
public class RepairrecordController {
    @Autowired
    private RepairrecordService repairrecordService;

    @GetMapping("/reapairrecordList")
    @ApiOperation("查询所有维修记录")
    public DataGridView selectAllReapairRecord(RepairrecordVo repairrecordVo) {
        Page<Object> page = PageHelper.startPage(repairrecordVo.getPage(), repairrecordVo.getLimit());
        List<Repairrecord> repairrecords = repairrecordService.selectAllReapairRecord(repairrecordVo);
        if (repairrecords != null && repairrecords.size() > 0) {
            return new DataGridView(page.getTotal(), repairrecords);
        } else {
            throw new CarException(ResultCode.REPAIRRECORDISNULL, Constaint.REPAIRRECORDISNULL);
        }
    }

    //1 分页查询
    @GetMapping("/repairrecords")
    @ApiOperation("分页查询所有的维修记录")
    public DataGridView selectAllBy(RepairrecordVo repairrecordVo) {
        Page<Object> page = PageHelper.startPage(repairrecordVo.getPage(), repairrecordVo.getLimit());
        List<Repairrecord> repairrecords = repairrecordService.selectAllBy(repairrecordVo);
        if (repairrecords != null && repairrecords.size() > 0) {
            return new DataGridView(page.getTotal(), repairrecords);
        } else {
            throw new CarException(ResultCode.OILRECORDISNULL, Constaint.OILRECORDISNULL);
        }
    }

    //2 增加
    @GetMapping("/addRepairRecord")
    @ApiOperation("增加维修记录")
    public R addRepairRecord(Repairrecord repairrecord, HttpServletRequest httpServletRequest) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //得到
        String vid = httpServletRequest.getParameter("vid");
//        String rsid = httpServletRequest.getParameter("rsid");
        String repairtime = httpServletRequest.getParameter("repairtime");
        String predictgetcar = httpServletRequest.getParameter("predictgetcar");
        String reason = httpServletRequest.getParameter("reason");
        String setcarinformation = httpServletRequest.getParameter("setcarinformation");
        String driverid = httpServletRequest.getParameter("driverid");
        String status = httpServletRequest.getParameter("status");//设置增加的状态
        //时间处理
        java.sql.Date st = null;
        java.sql.Date et = null;
        try {
            Date st1 = sdf.parse(repairtime);
            st = new java.sql.Date(st1.getTime());

            Date et1 = sdf.parse(predictgetcar);
            et = new java.sql.Date(et1.getTime());
        } catch (ParseException e) {
            e.printStackTrace();
        }
        //设置
        repairrecord.setVid(Integer.parseInt(vid));
//        repairrecord.setRsid(Integer.parseInt(rsid));
        repairrecord.setRepairtime(st);
        repairrecord.setRepairtime(et);
        repairrecord.setReason(reason);
        repairrecord.setSetcarinformation(setcarinformation);
        repairrecord.setDriverid(Integer.parseInt(driverid));
        repairrecord.setStatus(Integer.parseInt(status));
        int addRepairRecord = repairrecordService.addRepairRecord(repairrecord);
        if (addRepairRecord != 0) {
            return R.ok().code(ResultCode.REPAIRRECORDADDISSUC).message(Constaint.REPAIRRECORDADDISSUC);
        } else {
            throw new CarException(ResultCode.REPAIRRECORDADDISFAI, Constaint.REPAIRRECORDADDISFAI);
        }
    }

    //3 修改
    @GetMapping("/updateRepairRecord")
    @ApiOperation("修改维修记录")
    public R updateRepairRecord(Repairrecord repairrecord) {
        int updateRepairRecord = repairrecordService.updateRepairRecord(repairrecord);
        if (updateRepairRecord != 0) {
            return R.ok().code(ResultCode.REPAIRRECORDUPDATEISSUC).message(Constaint.REPAIRRECORDUPDATEISSUC);
        } else {
            throw new CarException(ResultCode.REPAIRRECORDUPDATEISFAI, Constaint.REPAIRRECORDUPDATEISFAI);
        }
    }

    //4 删除
    @GetMapping("/deleteRepairRecord")
    @ApiOperation("删除维修记录")
    public R deleteRepairRecord(Integer[] ids) {
        boolean deleteRepairRecord = repairrecordService.deleteRepairRecord(ids);
        if (deleteRepairRecord) {
            return R.ok().code(ResultCode.REPAIRRECORDDDELETEISSUC).message(Constaint.REPAIRRECORDDDELETEISSUC);
        } else {
            throw new CarException(ResultCode.REPAIRRECORDDELETEISFAI, Constaint.REPAIRRECORDDELETEISFAI);
        }

    }

    //5 配合修改
    @GetMapping("/repairrecordById")
    @ApiOperation("根据id查询维修记录")
    public R repairrecordById(Integer id) {
        Repairrecord repairrecordById = repairrecordService.repairrecordById(id);
        if (repairrecordById != null) {
            return R.ok().data("repairrecordById", repairrecordById);
        } else {
            throw new CarException(ResultCode.REPAIRRECORDISNULL, Constaint.REPAIRRECORDISNULL);
        }
    }

}

